import { createRouter, createWebHashHistory } from "vue-router"
import Drill from '../views/Drill'
import Training from '../views/Training'
import Test from '../views/hpDemo/Test'
import Exam from '../views/Exam'
import Public from '../views/Public'
import ShowTest from '../views/ShowTest'
import ShowHomework from '../views/ShowHomework'
import Testing from '../views/hpDemo/Testing.vue'
import Self from '../views/Self.vue'
import Login from '../views/Login.vue'
import Question from '../views/Question.vue'
import Problem from "@/views/Problem.vue";
import TrainQuestion from "@/views/TrainQuestion.vue";
import Self_classInformation from "@/views/Self_classInformation.vue";
import Self_collectedPost from "@/views/Self_collectedPost.vue";
import Self_posted from "@/views/Self_posted.vue"
import store from "@/store";
import ExamApi from"@/api/Exam"
import {ElMessage} from "element-plus";

const routes =[
    {
        path:'/drill',
        name:'Drill',
        component:Drill
    },
    {
        path:'/training',
        name:'Training',
        component:Training
    },
    {
        path:'/public',
        name:'Public',
        component:Public
    },
    {
        path:'/question',
        name:'Question',
        component:Question
    },

    {
        path:'/exam',
        name:'Exam',
        component:Exam,
    },
    {
        path:'/showtest',
        name:'ShowTest',
        component:ShowTest
    },
    {
        path:'/showhomework',
        name:'ShowHomework',
        component:ShowHomework
    },
    {
        path:'/problem',
        name:'Problem',
        component:Problem
    },
    {
        path:'/testing',
        name:'Testing',
        component:Testing
    },
    {
        path:'/self',
        name:'Self',
        component:Self,
        children:[

            {
                path:'',
                redirect:'/self/self_classInformation'
            },
            {
                path:'self_classInformation',
                component:Self_classInformation
            },
            {
                path:'self_collectedPost',
                component:Self_collectedPost
            },
            {
                path:'self_posted',
                component:Self_posted
            }
        ]
    },
    {
        path:'/login',
        component:Login
    },
    {
        path:'/',
        redirect:'/login'

    },
    {
        path:'/trainQuestion',
        name:'TrainQuestion',
        component:TrainQuestion
    },



]

const router =  createRouter({
    history: createWebHashHistory(),
    routes})
router.beforeEach((to, from, next) => {

    const token = localStorage.getItem("token")
    if (!token&& to.path !== '/login') {
        next('/login');
    }
    else {
        next();
    }
    if(from.path=="/showtest"){
        if(localStorage.getItem("examFlag")=="ing"){
            let parm={}
            parm.examId=localStorage.getItem("examId")
            parm.type="SKIP_PAGE"
            ExamApi.userBehavior(parm,token).then(res=>{
                if(res.data.success){
                    ElMessage.error("离开考试页面警告一次")
                }
            })
        }
        store.commit("user/logined")
    }
});
export default router
